﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ExternalConnections.CApECExternalConnectionDataExportConvertor
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System;


namespace DnProcessSimulateCommands.ExternalConnections;

internal class CApECExternalConnectionDataExportConvertor : IApECExternalConnectionDataConvertor
{
  private ITxPlcExternalConnectionDataEx m_data;

  internal CApECExternalConnectionDataExportConvertor()
  {
  }

  internal CApECExternalConnectionDataExportConvertor(ITxPlcExternalConnectionDataEx data)
  {
    this.m_data = data;
  }

  public IApECExternalConnectionDataConvertor CreateInstance(ITxPlcExternalConnectionDataEx data)
  {
    return (IApECExternalConnectionDataConvertor) new CApECExternalConnectionDataExportConvertor(data);
  }

  public ITxPlcExternalConnectionDataEx Data => this.m_data;

  public object Name
  {
    get => (object) this.m_data.Name;
    set
    {
      this.m_data.Name = value != null && value is string ? (value as string).Trim() : throw new ArgumentException();
    }
  }

  public object OpcServer
  {
    get
    {
      string opcServer = CApECExternalConnectionsManager.GetServerFromData(this.m_data);
      if (this.m_data is TxPlcSimAdvConnectionDataEx)
      {
        TxPlcSimAdvConnectionDataEx data = this.m_data as TxPlcSimAdvConnectionDataEx;
        if (!data.LocalConnection)
          opcServer = $"{opcServer}:{data.Port.ToString()}";
      }
      return (object) opcServer;
    }
    set
    {
      string serverString = value != null && value is string ? (value as string).Trim() : throw new ArgumentException();
      if (this.m_data is TxPlcSimAdvConnectionDataEx)
      {
        int length = serverString.LastIndexOf(':');
        if (length >= 0)
        {
          CApECExternalConnectionsManager.SetLocalPLCSIMAdvancedToData(false, this.m_data);
          CApECExternalConnectionsManager.SetServerPortToData(serverString.Substring(length + 1), this.m_data);
          serverString = serverString.Substring(0, length);
        }
        else
        {
          CApECExternalConnectionsManager.SetLocalPLCSIMAdvancedToData(true, this.m_data);
          CApECExternalConnectionsManager.SetServerPortToData("", this.m_data);
        }
      }
      CApECExternalConnectionsManager.SetServerToData(serverString, this.m_data);
    }
  }

  public object ItemHeader
  {
    get => (object) CApECExternalConnectionsManager.GetItemHeaderFromData(this.m_data);
    set
    {
      if (value == null || !(value is string))
        throw new ArgumentException();
      CApECExternalConnectionsManager.SetItemHeaderFromData(value as string, this.m_data);
    }
  }

  public object MapSignalsBy
  {
    get
    {
      return (object) CApECExternalConnectionsManager.GetMapSignalsTypeFromData(this.m_data, CApECExternalConnectionsManager.eConnectionDataFormat.Export);
    }
    set
    {
      if (value == null || !(value is string))
        throw new ArgumentException();
      CApECExternalConnectionsManager.SetMapSignalsTypeToData((value as string).Trim(), CApECExternalConnectionsManager.eConnectionDataFormat.Export, this.m_data);
    }
  }

  public object[] MapSignalsByOptions
  {
    get
    {
      return (object[]) CApECExternalConnectionsManager.GetMapSignalsTypeOptions(CApECExternalConnectionsManager.eConnectionDataFormat.Export);
    }
  }

  public object HardwareIndex
  {
    get
    {
      object hardwareIndex = (object) null;
      if (this.m_data is TxPlcSimbaConnectionDataEx)
        hardwareIndex = (object) (this.m_data as TxPlcSimbaConnectionDataEx).HardwareIndex;
      return hardwareIndex;
    }
    set
    {
      if (!(this.m_data is TxPlcSimbaConnectionDataEx))
        return;
      (this.m_data as TxPlcSimbaConnectionDataEx).HardwareIndex = (int) value;
    }
  }

  public object NameSpaceIndex
  {
    get => (object) CApECExternalConnectionsManager.GetNameSpaceIndexFromData(this.m_data);
    set
    {
      if (value == null || !(value is string))
        throw new ArgumentException();
      CApECExternalConnectionsManager.SetNameSpaceIndexToData(value as string, this.m_data);
    }
  }

  public object PluginState
  {
    get => (object) CApECExternalConnectionsManager.GetPluginStateFromData(this.m_data);
    set
    {
      if (value == null || !(value is string))
        throw new ArgumentException();
      CApECExternalConnectionsManager.SetPluginStateToData(value as string, this.m_data);
    }
  }

  public object TagAccessMode
  {
    get => (object) CApECExternalConnectionsManager.GetTagAccessFromData(this.m_data);
    set
    {
      if (value == null || !(value is string))
        throw new ArgumentException();
      CApECExternalConnectionsManager.SetTagAccessToData(value as string, this.m_data);
    }
  }

  public object InstanceName
  {
    get => (object) CApECExternalConnectionsManager.GetInstanceNameFromData(this.m_data);
    set
    {
      if (value == null || !(value is string))
        throw new ArgumentException();
      CApECExternalConnectionsManager.SetInstanceNameToData(value as string, this.m_data);
    }
  }

  public object COMElement
  {
    get => (object) CApECExternalConnectionsManager.GetCOMElementFromData(this.m_data);
    set
    {
      if (value == null || !(value is string))
        throw new ArgumentException();
      CApECExternalConnectionsManager.SetCOMElementToData(value as string, this.m_data);
    }
  }

  public object Type
  {
    get => (object) CApECExternalConnectionsManager.GetConnectionTypeFromData(this.m_data);
  }
}
